package com.haima.sage.bigdata.api.frame.condition.expression;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.haima.sage.bigdata.api.common.Constants;
import com.haima.sage.bigdata.api.frame.datatype.impl.BooleanDataTypeService;
import com.haima.sage.bigdata.api.frame.datatype.impl.ByteDataTypeService;
import com.haima.sage.bigdata.api.frame.datatype.impl.CharDataTypeService;
import com.haima.sage.bigdata.api.frame.datatype.impl.DateLongDataTypeService;
import com.haima.sage.bigdata.api.frame.datatype.impl.DoubleDataTypeService;
import com.haima.sage.bigdata.api.frame.datatype.impl.FloatDataTypeService;
import com.haima.sage.bigdata.api.frame.datatype.impl.IntDataTypeService;
import com.haima.sage.bigdata.api.frame.datatype.impl.LongDataTypeService;
import com.haima.sage.bigdata.api.frame.datatype.impl.ShortDataTypeService;
import com.haima.sage.bigdata.api.frame.datatype.impl.StringDataTypeService;


public abstract class BaseLogicService implements LogicService {
    protected SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATE_PATTERN);
    protected Object param;
    protected String strParam;
    protected String logic;

    protected Long dateParam;
    protected Double doubleParam;
    protected Float floatParam;
    protected Long longParam;
    protected Short shortParam;
    protected Integer intParam;
    protected Byte byteParam;
    protected Boolean boolParam;
    protected Character charParam;

    public BaseLogicService(Object param) {
        try {
            BooleanDataTypeService service = new BooleanDataTypeService();
            this.boolParam = service.trans(param);
        } catch (Exception e) {

        }
        try {
            DateLongDataTypeService service = new DateLongDataTypeService(Constants.DATE_PATTERN);
            this.dateParam = service.trans(param);
        } catch (Exception e) {

        }
        try {
            DoubleDataTypeService service = new DoubleDataTypeService();
            this.doubleParam = service.trans(param);
        } catch (Exception e) {

        }
        try {
            FloatDataTypeService service = new FloatDataTypeService();
            this.floatParam = service.trans(param);
        } catch (Exception e) {

        }
        try {
            LongDataTypeService service = new LongDataTypeService();
            this.longParam = service.trans(param);
        } catch (Exception e) {

        }
        try {
            IntDataTypeService service = new IntDataTypeService();
            this.intParam = service.trans(param);
        } catch (Exception e) {

        }
        try {
            ShortDataTypeService service = new ShortDataTypeService();
            this.shortParam = service.trans(param);
        } catch (Exception e) {

        }
        try {
            ByteDataTypeService service = new ByteDataTypeService();
            this.byteParam = service.trans(param);
        } catch (Exception e) {

        }
        try {
            CharDataTypeService service = new CharDataTypeService();
            this.charParam = service.trans(param);
        } catch (Exception e) {

        }

        try {
            StringDataTypeService service = new StringDataTypeService();
            this.strParam = service.trans(param);
        } catch (Exception e) {

        }

    }


    public boolean logic(Object object) throws Exception {
        if (object instanceof Timestamp) {
            return logic((Timestamp) object);
        } else if (object instanceof Date) {
            return logic((Date) object);
        } else if (object instanceof String) {
            return logic((String) object);
        } else if (object instanceof Character) {
            return logic((Character) object);
        } else if (object instanceof Byte) {
            return logic((Byte) object);
        } else if (object instanceof Short) {
            return logic((Short) object);
        } else if (object instanceof Integer) {
            return logic((Integer) object);
        } else if (object instanceof Long) {
            return logic((Long) object);
        } else if (object instanceof Float) {
            return logic((Float) object);
        } else if (object instanceof Double) {
            return logic((Double) object);
        } else if (object instanceof Boolean) {
            return logic((Boolean) object);
        }
        return false;
    }

    protected boolean logicNotEmpty(String object) throws Exception {
        return true;
    }

    protected boolean logicNotEmpty(Date object) throws Exception {
        return true;
    }

    protected boolean logicNotEmpty(Byte object) throws Exception {
        return true;
    }

    protected boolean logicNotEmpty(Short object) throws Exception {
        return true;
    }

    protected boolean logicNotEmpty(Integer object) throws Exception {
        return true;
    }

    protected boolean logicNotEmpty(Long object) throws Exception {
        return true;
    }

    protected boolean logicNotEmpty(Float object) throws Exception {
        return true;
    }

    protected boolean logicNotEmpty(Double object) throws Exception {
        return true;
    }

    protected boolean logicNotEmpty(Boolean object) throws Exception {
        return true;
    }

    protected boolean logicNotEmpty(Character object) throws Exception {
        return true;
    }

    public boolean logic(String objectparam) throws Exception {
        if (objectparam == null) {
            return false;
        }
        return logicNotEmpty(objectparam);
    }

    public boolean logic(Boolean objectparam) throws Exception {
        if (objectparam == null) {
            return false;
        }
        return logicNotEmpty(objectparam);
    }

    public boolean logic(Character objectparam) throws Exception {
        if (objectparam == null) {
            return false;
        }
        return logicNotEmpty(objectparam);
    }

    public boolean logic(Byte objectparam) throws Exception {
        if (objectparam == null) {
            return false;
        }
        return logicNotEmpty(objectparam);
    }

    public boolean logic(Short objectparam) throws Exception {
        if (objectparam == null) {
            return false;
        }
        return logicNotEmpty(objectparam);
    }

    public boolean logic(Integer objectparam) throws Exception {
        if (objectparam == null) {
            return false;
        }
        return logicNotEmpty(objectparam);
    }

    public boolean logic(Long objectparam) throws Exception {
        if (objectparam == null) {
            return false;
        }
        return logicNotEmpty(objectparam);
    }

    public boolean logic(Float objectparam) throws Exception {
        if (objectparam == null) {
            return false;
        }
        return logicNotEmpty(objectparam);
    }

    public boolean logic(Double objectparam) throws Exception {
        if (objectparam == null) {
            return false;
        }
        return logicNotEmpty(objectparam);
    }

    public boolean logic(Date objectparam) throws Exception {
        if (objectparam == null) {
            return false;
        }
        return logicNotEmpty(objectparam);
    }

    public boolean logic(Timestamp objectparam) throws Exception {
        if (objectparam == null) {
            return false;
        }
        return logicNotEmpty(objectparam);
    }

    public Object getParam() {
        return param;
    }

    public void setParam(Object param) {
        this.param = param;
    }

    public String getLogic() {
        return logic;
    }

    public void setLogic(String logic) {
        this.logic = logic;
        if (this.logic != null) {
            this.logic = this.logic.trim().toLowerCase().replace("-", "");
        }
    }
}
